package com.example.SBootDemo.service.Imp.function;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.example.SBootDemo.Dao.Interface.funDao;
import com.example.SBootDemo.Dao.Interface.roleDao;
import com.example.SBootDemo.Dao.Interface.userDao;
import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.Util.PageInfo;
import com.example.SBootDemo.Util.TableTitles;
import com.example.SBootDemo.Util.funplace;
import com.example.SBootDemo.model.function.user;
import com.example.SBootDemo.service.Interface.function.userinterface;
import com.example.SBootDemo.model.function.role;
@Service
public class userimpl implements userinterface {
	@Autowired
	userDao us;
	@Autowired
	roleDao role;
	@Autowired
	funDao fun;
	@Override
	public int addUser(user user) {
		// TODO Auto-generated method stub
		int flag=0;
		try {
			us.save(user);
			flag=1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	@Transactional
	public void deleteUser(List<Integer> user) throws Myexception {
		// TODO Auto-generated method stub
		try {
			for(int i=0;i<user.size();i++){
				us.deleteById(user.get(i));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("删除用户出错");
		}
	}

	@Override
	public user getUser(int id) {
		// TODO Auto-generated method stub
		
		return us.findById(id);
	}

	//分页查询用户s
	@Override
	public int getUsers(int pageinfo,HttpServletRequest request) throws Myexception   {
		// TODO Auto-generated method stub
		int flag=0;
		
		Sort sort = new Sort(Direction.DESC, "id");
	    Pageable pageable = new PageRequest(pageinfo, TableTitles.getPagesize(), sort);
	    Page page = null;
		
			try {
				page = us.findAll(pageable);
				flag=1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new Myexception("分页查询user方法出错");
			}
			 
		    //面包屑导航
	    funplace funplace = new funplace("系统管理","用户与权限","用户列表");
	    List<String> titles = TableTitles.getUserTitles();
	    //列表信息
		 request.setAttribute("List", page.getContent());
		//列表栏目信息
		 request.setAttribute("titles", titles);
		 //分页总页数
		 request.setAttribute("pages", page.getTotalPages());
		 //当前页，spring date jpa默认从0算起
		 request.setAttribute("number", page.getNumber());
	   
		 request.setAttribute("funplace", funplace);
		return  flag;
	}
	//跳转到添加用户页面的信息准备
	@Override
	public void addpage(HttpServletRequest request) {
		// TODO Auto-generated method stub
		  funplace funplace = new funplace("系统管理","用户与权限","用户添加");
		  request.setAttribute("funplace", funplace);
		  List<role> roles=role.findAll();
			
		  //空user 前台模板取null有用
		  request.setAttribute("user", new user());
		  //前段角色下拉
		  request.setAttribute("roles", roles);
	}
	//跳转到修改用户页面的信息准备
	@Override
	@Transactional
	public void updatepage(HttpServletRequest request,int id) {
		// TODO Auto-generated method stub
		 user user = us.findById(id);
		 funplace funplace = new funplace("系统管理","用户与权限","用户修改");
		 List<role> roles=role.findAll();
		 request.setAttribute("roles", roles);
	     request.setAttribute("funplace", funplace);
		 request.setAttribute("user", user);
	}

	
	//用户信息修改
	@Override
	public void updateUser(user user,HttpServletRequest request) throws Myexception {
		role uprole =role.findByRolename(user.getRole().getRolename()) ;
		user.setRole(uprole);
		try {
			us.save(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("更新用户信息方法出错");
		}
		// TODO Auto-generated method stub
		
	}
/******************登陆********************************************************/
	@Override
	public user findByUsername(String username) {
		// TODO Auto-generated method stub
		user user = us.findByUsername(username);
		if(user.getRole().getIfadmin()==1){
			user.getRole().setFunlist(fun.findAll());
		}
		return user;
	}

}
